﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Api.BLL;
using Api.Common;
using Api.Model;
using Api.Web.helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Api.Web.Areas.Admin.Controllers
{
    public class MethodController : AdminController
    {
        public ActionResult List(int projectId = 0)
        {
            if (projectId <= 0)
            {
                ViewBag.Error = "參數錯誤";
                return View();
            }
            return View();
        }

        public JsonResult AjaxList(int categoryId = 0)
        {
            var result = from m in Db.Current.Methods
                         join c in Db.Current.Categories.Where(s => s.UserId == userId) on m.ProjectId equals c.ProjectId into g
                         where m.ProjectId == projectId && m.UserId == userId
                            && (categoryId == -1 ? !g.Any(s => s.Id == m.CategoryId) : categoryId == 0 ? true : m.CategoryId == categoryId)
                         select new
                         {
                             m.CategoryId,
                             m.Id,
                             m.Name,
                             m.HttpMethod,
                             Categorys = g.Where(s => s.ProjectId == projectId && s.UserId == userId).Select(s => new
                             {                                 
                                 MethodId = m.Id,
                                 Selected = s.Id == m.CategoryId,
                                 s.Name,
                                 s.Id,
                             })
                         };

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public void Edit(Method method)
        {
            if (method.ProjectId == 0)
            {
                AjaxMsgHelper.ResponseFail("缺少projectId");
                return;
            }
            bool flag = true;
            if (!string.IsNullOrEmpty(method.Name))
            {
                if (method.Id > 0)
                {
                    try
                    {
                        MethodBLL.Current.UpdateIgnore(s => new { s.CreateDate, s.ProjectId, s.CategoryId, s.UserId }, method);
                    }
                    catch
                    {
                        flag = false;
                    }
                }
                else
                {
                    try
                    {

                        method.ProjectId = projectId;
                        method.UserId = userId;
                        method.CreateDate = DateTime.Now;
                        MethodBLL.Current.Add(method);
                    }
                    catch
                    {
                        flag = false;
                    }
                }

                if (flag)
                {
                    AjaxMsgHelper.ResponseSuccess("", null, Url.Action("List", new { Area = "Admin", projectId = method.ProjectId }));
                }
                else
                {
                    AjaxMsgHelper.ResponseFail("操作失敗，請重試");
                }
            }
            else
            {
                AjaxMsgHelper.ResponseFail("請填寫名稱");
            }
        }
        public ActionResult Create(int? projectId)
        {
            if (projectId == null)
            {
                ViewBag.Error = "參數錯誤";
                return View();
            }
            return View();
        }
        public ActionResult Edit(int? projectId, int? id)
        {
            if (id == null || projectId == null)
            {
                ViewBag.Error = "參數錯誤";
                return View("Create");
            }
            Method Method = MethodBLL.Current.GetModel(s => s.Id == id && s.ProjectId == projectId && s.UserId == userId);
            if (Method == null)
            {
                ViewBag.Error = "數據不存在";
                return View("Create");
            }
            return View("Create", Method);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public void Delete(params int[] ids)
        {
            if (ids.Length > 0)
            {
                var list = MethodBLL.Current.GetList(s => ids.Contains(s.Id) && s.UserId == userId);
                if (list == null)
                {
                    AjaxMsgHelper.ResponseFail("操作失敗");
                    return;
                }
                bool result = true;
                try
                {
                    MethodBLL.Current.DeleteList(list);
                }
                catch
                {
                    result = false;
                }
                if (result)
                {
                    AjaxMsgHelper.ResponseSuccess("刪除成功");
                }
                else
                {
                    AjaxMsgHelper.ResponseFail("操作失敗");
                }
            }
            else
            {
                AjaxMsgHelper.ResponseFail("請選擇一項");
            }
        }

        public void ModifyCategory(int? methodId, int? categoryId)
        {
            if (methodId == null || categoryId == null)
            {
                AjaxMsgHelper.ResponseFail("參數錯誤");
                return;
            }
            var model = MethodBLL.Current.GetModel(s => s.Id == methodId && s.UserId == userId);
            if (model != null)
            {

                try
                {
                    MethodBLL.Current.Update(model, new { CategoryId = categoryId.Value });
                    AjaxMsgHelper.ResponseSuccess("操作成功");
                }
                catch
                {
                    AjaxMsgHelper.ResponseFail("操作失敗");
                }
            }
            else
            {
                AjaxMsgHelper.ResponseFail("數據不存在,請刷新重試");
            }
        }
    }
}